
/**
 * 详情对话框
 */
var InfoDlg = {
    data: {
        isSort: null,
        nodes: [],
        isCheck: null,
        ids: null
    }
};

layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var Role = {
        tableId: "roleTable",    //表格id
        condition: {
            pid: '',
            searchWords: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Role.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'roleId', hide: true, sort: true, title: '角色id'},
            {field: 'name', sort: true, title: '角色名称'},
            {field: 'pname', sort: true, title: '上级角色'},
            {field: 'description', sort: true, title: '详情描述'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 120}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Role.search = function () {
        var queryData = {};
        queryData['searchWords'] = $("#searchWords").val();
        table.reload(Role.tableId, {where: queryData});
    };

    /**
     * 弹出添加角色
     */
    Role.openAddRole = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加角色',
            area: ['40%', '60%'],
            content: Lu.ctxPath + '/system/role/toAdd',
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
    };

    /**
     * 点击编辑角色
     *
     * @param data 点击按钮时候的行数据
     */
    Role.onEditRole = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改角色',
            area: ['40%', '60%'],
            content: Lu.ctxPath + '/system/role/toUpdate?roleId=' + data.roleId,
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
    };

    /**
     * 点击删除角色
     *
     * @param data 点击按钮时候的行数据
     */
    Role.onDeleteRole = function (data) {
        var operation = function () {
            var ajax = new $ax(Lu.ctxPath + "/system/role/delete", function () {
                Lu.success("删除成功!");
                table.reload(Role.tableId);
            }, function (data) {
                Lu.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("roleId", data.roleId);
            ajax.start();
        };
        Lu.confirm("是否删除角色 " + data.name + "?", operation);
    };

    /**
     * 分配菜单
     *
     * @param data 点击按钮时候的行数据
     */
    Role.roleAssign = function (data) {
        var roleId = data.roleId;
        var isCheck = encodeURIComponent('parent.InfoDlg.data.isCheck');
        var ids = encodeURIComponent('parent.InfoDlg.data.ids');
        var treeUrl = encodeURIComponent('/system/menu/menuTreeListByRoleId/' + roleId);
        layer.open({
            type: 2,
            title: '权限配置',
            area: ['40%', '90%'],
            content: '/system/treeCheck?isCheck='+isCheck+'&ids='+ids+'&treeUrl='+treeUrl+'',
            end: function () {
                var isCheck = InfoDlg.data.isCheck;
                var ids = InfoDlg.data.ids;
                if(isCheck && ids && isCheck == 1){
                    //给角色分配权限
                    var ajax = new $ax(Lu.ctxPath + "/system/role/setAuthority", function (data) {
                        Lu.success("分配角色成功!");
                        InfoDlg.data.isCheck = null;
                        InfoDlg.data.ids = null;
                    }, function (data) {
                        Lu.error("分配角色失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("ids", ids);
                    ajax.set("roleId", roleId);
                    ajax.start();
                }
            }
        });

    };

    /**
     * 点击排序按钮
     *
     * @param data 点击按钮时候的行数据
     */
    Role.onSortRole = function (data) {
        var isSort = encodeURIComponent('parent.InfoDlg.data.isSort');
        var nodes = encodeURIComponent('parent.InfoDlg.data.nodes');
        var treeUrl = encodeURIComponent('/system/role/roleTreeList');
        layer.open({
            type: 2,
            title: '父级菜单',
            area: ['40%', '90%'],
            content: '/system/treeSort?isSort='+isSort+'&nodes='+nodes+'&treeUrl='+treeUrl+'',
            end: function () {
                var isSort = InfoDlg.data.isSort;
                var nodes = InfoDlg.data.nodes;
                if(isSort && nodes && isSort == 1){
                    //排序
                    var ajax = new $ax(Lu.ctxPath + "/system/role/sort", function () {
                        Lu.success("排序成功!");
                        InfoDlg.data.isSort = null;
                        InfoDlg.data.nodes = null;
                        table.reload(Role.tableId);
                    }, function (data) {
                        Lu.error("排序失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("nodes", JSON.stringify(nodes));
                    ajax.start();
                }
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Role.tableId,
        url: Lu.ctxPath + '/system/role/listPage',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Role.initColumn()
    });

    var tableReload = () => {table.reload(Role.tableId);};

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Role.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Role.openAddRole();
    });

    //菜单排序
    $('#sort').click(function () {
        Role.onSortRole();
    });

    // 工具条点击事件
    table.on('tool(' + Role.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'update') {
            Role.onEditRole(data);
        } else if (layEvent === 'delete') {
            Role.onDeleteRole(data);
        } else if (layEvent === 'roleAssign') {
            Role.roleAssign(data);
        }
    });
});
